<?php

class ForeseeData {
    public static function saveForesee($name, $value) {
        $name = mysql_escape_string($name);
        $value = mysql_escape_string($value);
        $sqlResult = DB::query("INSERT INTO foresee_hystory (`name`, `date`, `value`) VALUES ('$name', NOW(), '$value')");
        if (!$sqlResult) {
            return false;
        }
        return mysql_insert_id();
    }
    
    public static function deleteForesee($id) {
        $id = intval($id);
        DB::query("DELETE FROM foresee_hystory WHERE id = $id");
    }
    
    public static function hasSavedElements() {
        $sql_result = DB::query("SELECT COUNT(*) FROM foresee_hystory");
        return current(DB::row($sql_result));
    }
    
    public static function saveLastPrediction($value) {
        if (is_array($value)) {
            $value = serialize($value);
        }
        $value = mysql_escape_string($value);
        if (current(DB::row(DB::query("SELECT COUNT(*) FROM foresee_hystory WHERE name IS NULL")))) {
            DB::query("UPDATE foresee_hystory SET `value` = '$value' WHERE name IS NULL");
        } else {
            DB::query("INSERT foresee_hystory (`name`, `value`) VALUES (NULL, '$value')");
        }
        
    }
}